/**
 * 题库相关API
 */
import { get, post, put, del } from './index'
import type {
  ApiResponse,
  PaginatedResponse,
  PaginationParams,
  QuestionBank,
  Question,
  MediaFile
} from '@/types'

// 题库管理
export const getQuestionBanks = (params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<QuestionBank>>>('/api/v1/questions/question-banks/', params)
}

export const getQuestionBank = (id: number) => {
  return get<ApiResponse<QuestionBank>>(`/api/v1/questions/question-banks/${id}/`)
}

export const createQuestionBank = (data: Partial<QuestionBank>) => {
  return post<ApiResponse<QuestionBank>>('/api/v1/questions/question-banks/', data)
}

export const updateQuestionBank = (id: number, data: Partial<QuestionBank>) => {
  return put<ApiResponse<QuestionBank>>(`/api/v1/questions/question-banks/${id}/`, data)
}

export const deleteQuestionBank = (id: number) => {
  return del(`/api/v1/questions/question-banks/${id}/`)
}

// 题目管理
export const getQuestions = (params?: any) => {
  return get<ApiResponse<PaginatedResponse<Question>>>('/api/v1/questions/questions/', params)
}

export const getQuestion = (id: string | number) => {
  return get<ApiResponse<Question>>(`/api/v1/questions/questions/${id}/`)
}

export const createQuestion = (data: Partial<Question>) => {
  return post<ApiResponse<Question>>('/api/v1/questions/questions/', data)
}

export const updateQuestion = (id: string | number, data: Partial<Question>) => {
  return put<ApiResponse<Question>>(`/api/v1/questions/questions/${id}/`, data)
}

export const deleteQuestion = (id: number) => {
  return del(`/api/v1/questions/questions/${id}/`)
}

// 批量操作
export const batchImportQuestions = (questionBankId: number, data: { questions: any[] }) => {
  return post<ApiResponse<{ imported: number; skipped: number; errors: string[] }>>(
    `/api/v1/questions/question-banks/${questionBankId}/batch-import/`,
    data
  )
}

export const batchExportQuestions = (params: { question_bank?: number; question_ids?: number[] }) => {
  return post('/api/v1/questions/questions/batch-export/', params)
}

export const batchDeleteQuestions = (data: { question_ids: number[] }) => {
  return post('/api/v1/questions/questions/batch-delete/', data)
}

// 题目统计
export const getQuestionStatistics = (questionBankId?: number) => {
  const url = questionBankId
    ? `/questions/question-banks/${questionBankId}/statistics/`
    : '/questions/questions/statistics/'
  return get<ApiResponse<any>>(url)
}

// 智能组卷
export const generatePaper = (data: {
  question_bank: number
  question_count: number
  difficulty_distribution: Record<string, number>
  type_distribution: Record<string, number>
  tags?: string[]
}) => {
  return post<ApiResponse<{ questions: Question[]; paper_info: any }>>(
    '/questions/questions/generate-paper/',
    data
  )
}

// 文件上传
export const uploadMediaFile = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return post<ApiResponse<MediaFile>>('/questions/upload-media/', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 题目搜索
export const searchQuestions = (params: {
  keyword?: string
  question_bank?: number
  question_type?: string
  difficulty?: string
  tags?: string[]
}) => {
  return get<ApiResponse<PaginatedResponse<Question>>>('/questions/questions/search/', params)
}

// 获取题目标签
export const getQuestionTags = (questionBankId?: number) => {
  const url = questionBankId
    ? `/api/v1/questions/question-banks/${questionBankId}/tags/`
    : '/api/v1/questions/questions/tags/'
  return get<ApiResponse<string[]>>(url)
}

// 导出统一的题目API对象
export const questionApi = {
  // 题库相关
  getQuestionBanks,
  getQuestionBank,
  createQuestionBank,
  updateQuestionBank,
  deleteQuestionBank,

  // 题目相关
  getQuestions,
  getQuestion,
  createQuestion,
  updateQuestion,
  deleteQuestion,

  // 批量操作
  batchImportQuestions,
  batchExportQuestions,
  batchDeleteQuestions,

  // 其他功能
  getQuestionStatistics,
  generatePaper,
  uploadMediaFile,
  searchQuestions,
  getQuestionTags
}